<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording" @change="handleSearchChange" ref="searchuinew"></searchuinew>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <div class="panel-line">
          <el-button size="mini" @click="handleSendRecord">发送记录</el-button>
          <el-button size="mini" @click="handlePlanRecord">计划列表</el-button>
          <el-button size="mini" type="primary" @click="handleSendMsg">新建发送计划</el-button>
        </div>
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column align="center" header-align="center" label="公司名称" prop="company" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="公司简介" prop="desc"></el-table-column>
          <el-table-column align="center" header-align="center" label="姓名" prop="name"></el-table-column>
          <el-table-column align="center" header-align="center" label="电话" prop="phone" width="150"></el-table-column>
          <el-table-column align="center" header-align="center" label="来源" prop="source" width="150"></el-table-column>
          <el-table-column align="center" header-align="center" label="公司类型" prop="com_type"></el-table-column>
          <el-table-column align="center" header-align="center" label="地址" prop="address"></el-table-column>
          <el-table-column align="center" header-align="center" label="分类" prop="cate_name"></el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>

    <!--  发送短信 -->
    <el-dialog title="发送短信" :visible.sync="showSendMsg" width="60%">
      <SendMsg v-if="showSendMsg" :searchParams="searchParams" :total="totalPage" @close="showSendMsg = false" />
    </el-dialog>

    <!--  发送记录 -->
    <el-dialog title="发送记录" :visible.sync="showMsgRecord" width="80%">
      <MsgRecord v-if="showMsgRecord" :id="currentId" :searchParams="searchParams" @close="showMsgRecord = false" />
    </el-dialog>

    <!--  计划列表 -->
    <el-dialog title="计划列表" :visible.sync="showPlanRecord" width="80%">
      <PlanRecord v-if="showPlanRecord" :searchParams="searchParams" @close="showPlanRecord = false" @showPlanDetail="handleShowPlanDetail" @showSendRecord="handleSendRecord" />
    </el-dialog>

    <!--  计划详情 -->
    <el-dialog title="计划详情" :visible.sync="showPlanDetail" width="60%">
      <PlanDetail v-if="showPlanDetail" :id="currentId" @close="showPlanDetail = false" />
    </el-dialog>
  </div>
</template>

<script>
import { apiCRMList, apiCRMSelect } from '@/api/assetsDisposal'
import SendMsg from './components/SendMsg'
import MsgRecord from './components/MsgRecord'
import PlanRecord from './components/PlanRecord'
import PlanDetail from './components/PlanDetail'

export default {
  name: 'index',
  components: { SendMsg, MsgRecord, PlanRecord, PlanDetail },
  data() {
    return {
      searchFormArr: [
        {
          fieldName: '姓名：',
          type: 'input',
          field: 'name',
          placeholder: '请输入姓名',
        },
        {
          fieldName: '电话：',
          type: 'input',
          field: 'phone',
          placeholder: '请输入电话',
        },
        {
          fieldName: '分类：',
          type: 'select',
          field: 'cate_id',
          option: [],
          placeholder: '请选择',
        },
        {
          fieldName: '省：',
          type: 'select',
          field: 'sheng_id',
          option: [],
          filterable: true,
          placeholder: '请选择',
        },
        {
          fieldName: '市：',
          type: 'select',
          field: 'shi_id',
          option: [],
          filterable: true,
          placeholder: '请选择',
        },
      ],
      isAccording: true,
      searchParams: {
        name: '',
        phone: '',
        cate_id: '',
        sheng_id: '',
        shi_id: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,

      showSendMsg: false,
      showMsgRecord: false,
      showPlanRecord: false,
      zoneList: [],
      showPlanDetail: false,
      currentId: '',
    }
  },
  mounted() {
    this.getSelect()
    this.getDataList()
  },
  methods: {
    // 获取筛选数据
    getSelect() {
      apiCRMSelect().then((res) => {
        let { cate, zone } = res.data
        cate = cate.map((item) => {
          return {
            id: item.id,
            text: item.name,
          }
        })
        this.searchFormArr.find((item) => item.field === 'cate_id').option = cate

        const sheng = zone.map((item) => {
          return {
            id: item.code,
            text: item.name,
          }
        })
        this.searchFormArr.find((item) => item.field === 'sheng_id').option = sheng
        const shi =
          sheng[0].children &&
          sheng[0].children.map((item) => {
            return {
              id: item.code,
              text: item.name,
            }
          })
        this.searchFormArr.find((item) => item.field === 'shi_id').option = shi
        this.zoneList = zone
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams)
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      apiCRMList(formData)
        .then((res) => {
          const { items, total } = res.result
          this.dataList = items
          this.totalPage = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 筛选的省市联动
    handleSearchChange({ item, fieldArr }) {
      if (item.field === 'sheng_id') {
        this.$refs.searchuinew.clearFormItem('shi_id')
        this.searchParams.shi_id = ''
        const option = this.zoneList.find((item) => {
          return item.code === fieldArr.sheng_id
        })
        if (option) {
          let shi = []
          if (option.children) {
            shi = option.children.map((item) => {
              return {
                id: item.code,
                text: item.name,
              }
            })
          }
          this.searchFormArr.find((item) => item.field === 'shi_id').option = shi
        }
      }
    },
    // 列表查询
    searchParma(e) {
      console.log(e)
      this.searchFormArr.forEach((item) => {
        if (item.field === 'time') return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 刷新列表
    handleRefresh() {
      this.showOfflineSign = false
      this.getDataList()
    },

    // 发送短信
    handleSendMsg() {
      this.showSendMsg = true
    },
    // 发送记录
    handleSendRecord(id) {
      if (id && typeof id === 'string') {
        this.currentId = id
      } else {
        this.currentId = ''
      }
      this.showMsgRecord = true
    },
    // 计划记录
    handlePlanRecord() {
      this.showPlanRecord = true
    },
    // 计划详情
    handleShowPlanDetail(id) {
      this.currentId = id
      this.showPlanDetail = true
    },
  },
}
</script>

<style scoped lang="scss">
.panel-line {
  margin-bottom: 16px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.text-1 {
  .link {
    color: #409eff;
    cursor: pointer;
  }
}
</style>
